<?php
/**
 * Description of userclass
 *
 * @author Machiel
 */
class UserException extends Exception {

}
class User {
    /**
     *
     * @var Character
     */
    private $characters = array();
    private $loadedCharacters = false;

    private $id;
    private $email;
    private $password;

    /**
     *
     * @var Date
     */
    private $registerDate;

    function  __construct($id, $email, $password, Date $registerDate) {
        $this->id = $id;
        $this->email = $email;
        $this->password = $password;
        $this->registerDate = $registerDate;
    }

    private function loadCharacters() {
        if(!$this->loadedCharacters) {
            global $qm;
            $this->loadedCharacters = true;
            $this->characters = $qm->getCharactersFromUserId($this->id);
        }
    }

    public function getCharacters() {
        $this->loadCharacters();
        return $this->characters;
    }

    public function getCharacter($characterId) {
        $this->loadCharacters();
        
        if(!is_numeric($characterId)) {
            throw new UserException("Character ID is not numeric");
        }

        if(!isset($this->characters[$characterId])) {
            throw new UserException("Character ID not found.");
        }

        return $this->characters[$characterId];
    }
}
?>
